<<<<<<< HEAD
from . import admin_bp 
from flask import request,jsonify
from app.models import User,UserProfile,OperationLog,ModuleType,OperationType
=======
from . import admin_bp
from flask import request, jsonify
from app.models import User, UserProfile, OperationLog, ModuleType, OperationType
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
from app.extensions import db
from datetime import datetime


<<<<<<< HEAD
@admin_bp.route('/user_profile',methods=['GET'])
=======
@admin_bp.route('/user_profile', methods=['GET'])
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
def user_profile():
    try:
        id = request.args.get('id')
        user = User.query.get(id)
<<<<<<< HEAD
        create_time  = (datetime.now() - user.create_time).days
        profile = user.profile
        data = {
            'profile':profile.to_json(),
            'create_time':create_time
        }
          
        return jsonify({'message':'获取用户信息成功','errcode':'0','data':data}),200
    except Exception as e:
        print(e)
        return jsonify({'message':'获取用户信息失败','errcode':'-1','error':str(e)}),500


@admin_bp.route('/user_profile/edit',methods=['PUT'])
=======
        create_time = (datetime.now() - user.create_time).days
        profile = user.profile
        data = {
            'profile': profile.to_json(),
            'create_time': create_time
        }

        return jsonify({'message': '获取用户信息成功', 'errcode': '0', 'data': data}), 200
    except Exception as e:
        print(e)
        return jsonify({'message': '获取用户信息失败', 'errcode': '-1', 'error': str(e)}), 500


@admin_bp.route('/user_profile/edit', methods=['PUT'])
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
def user_profile_edit():
    id = request.json.get('id')
    name = request.json.get('name')
    phone = request.json.get('phone')
    gender = request.json.get('gender')
<<<<<<< HEAD
    birthday = request.json.get('birthday') if request.json.get('birthday') else None
    address = request.json.get('address')
    uid = request.json.get('uid')
    
=======
    birthday = request.json.get(
        'birthday') if request.json.get('birthday') else None
    address = request.json.get('address')
    uid = request.json.get('uid')

>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
    user_info = UserProfile.query.get(id)
    user_info.name = name
    user_info.phone = phone
    user_info.gender = gender
    user_info.birthday = birthday
    user_info.address = address
    try:
        db.session.commit()
        uid_user = User.query.get(uid)
<<<<<<< HEAD
        operation_log = OperationLog(user_id=uid,email=uid_user.email,module=ModuleType.USER_PROFILE,operation=OperationType.UPDATE,detail=f'更新 {uid_user.email} 的个人信息')
        db.session.add(operation_log)
        db.session.commit()
        return jsonify({'message':'更新用户信息成功','errcode':'0'}),200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message':'更新用户信息失败','errcode':'-1','error':str(e)}),500

=======
        operation_log = OperationLog(user_id=uid, email=uid_user.email, module=ModuleType.USER_PROFILE,
                                     operation=OperationType.UPDATE, detail=f'更新 {uid_user.email} 的个人信息')
        db.session.add(operation_log)
        db.session.commit()
        return jsonify({'message': '更新用户信息成功', 'errcode': '0'}), 200
    except Exception as e:
        db.session.rollback()
        return jsonify({'message': '更新用户信息失败', 'errcode': '-1', 'error': str(e)}), 500
>>>>>>> 7245172afa54706ac2f9973f27248496e8835d1c
